-- 延时打击

local HumanBone = TaleWorlds.Core.HumanBone
local AgentAttackType = TaleWorlds.Core.AgentAttackType
local Color = TaleWorlds.Library.Color
local SkinVoiceManager = TaleWorlds.MountAndBlade.SkinVoiceManager

local onlyPlayer = true
local bones = System.Enum.GetValues(HumanBone.Abdomen:GetType())
local behavior

return {
onBeforeMissionBehaviorInitialize = function (mission)
    behavior = SubModule:CreateMissionBehavior({
        onAgentHit = function (affectedAgent, affectorAgent, affectorWeapon, blow, attackCollisionData)
            if blow.IsMissile or not affectedAgent.IsHuman or affectedAgent == affectorAgent or affectorAgent == nil or (onlyPlayer and not affectorAgent.IsMainAgent) or affectedAgent.Health <= 0 or blow.InflictedDamage < 5 then
                return
            end

            blow.OwnerId = -1
            blow.InflictedDamage = blow.InflictedDamage / 5
            blow.AttackType = AgentAttackType.Collision

            for i = 1, 5, 1 do
                behavior:SetTimeout(1000 + 300 * i, function (dt)
                    if i == 1 then
                        if affectorAgent.IsMainAgent then
                            InformationManager.DisplayMessage(InformationMessage("延时打击！", Color(0.372, 0.898, 0.305)))
                        end
                        affectorAgent:MakeVoice(SkinVoiceManager.VoiceType.MpSorry, SkinVoiceManager.CombatVoiceNetworkPredictionType.NoPrediction)
                    end
                    if affectedAgent.Health <= 0 then
                        return
                    end
                    local bone = bones[MBRandom.RandomInt(bones.Length)]
                    SubModule:CreateParticleSystemAttachedToBone(affectedAgent.AgentVisuals, "psys_game_blood_1", bone)
                    SubModule:CreateParticleSystemAttachedToBone(affectedAgent.AgentVisuals, "psys_game_missile_sand_coll", bone)
                    affectedAgent:RegisterBlow(blow, attackCollisionData)
                end)
            end
        end
    })
    mission:AddMissionBehavior(behavior)
end
}